﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using System.Data;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
    public partial class StatisticsHospitalByMonth : BasePage
    {
        DataTable dt = new DataTable();
        public string Hid;
        int year;
        int month;

        public StatisticsHospitalByMonth()
        {
            this.ModuleNo = Module.报表统计.ToString();
            this.PageOperateType = OperateType.医院月报.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Hid = Request["Y"];
            if (!IsPostBack)
            {
                year = DateTime.Now.Year;
                foreach (ListItem item in this.droYear.Items)
                {
                    //判断是否是当前的年份
                    if (item.Text == year.ToString())   //2011.4.12 张雄 modify
                    {
                        //如果是的则显示当前的年份
                        item.Selected = true;
                        break;
                    }
                }
                //by张雄    modify  
                month = DateTime.Now.Month;
                foreach (ListItem item in this.droMonth.Items)
                {
                    if (item.Text == month.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }

                if (DateTime.Now.Month != 1)
                {
                    month = DateTime.Now.Month - 1;
                }
                else
                {
                    year = DateTime.Now.Year - 1;
                    month = 12;
                }

                dataBind();
                BindMSChat();
                GetStatistics();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        private void dataBind()
        {
            string hospitalId = string.Intern(Hid);

            string Year = string.Intern(this.droYear.SelectedItem.Text);

            string Month = string.Intern(this.droMonth.SelectedValue);

            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            dt = ie.StatisticsHospitalByMonth(hospitalId, Year, Month);

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");//增加新列，主要目的是格式化显示字段

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

            gvwStatisticsHos.DataSource = dt;

            gvwStatisticsHos.DataBind();

            SpanCell();
        }

        /// <summary>
        /// 合并单元格
        /// 2011.4.10 张雄 create
        /// </summary>
        private void SpanCell()
        {
            for (int j = 0; j < gvwStatisticsHos.Rows.Count; j++)
            {
                if (j < gvwStatisticsHos.Rows.Count)
                {
                    //第一行与第二行相等
                    if ((gvwStatisticsHos.Rows.Count - j) >= 2 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 1].Cells[0].Text)
                    {
                        //第一行与第三行相等
                        if ((gvwStatisticsHos.Rows.Count - j) >= 3 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 2].Cells[0].Text)
                        {
                            //第一行与第四行相等
                            if ((gvwStatisticsHos.Rows.Count - j) >= 4 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 3].Cells[0].Text)
                            {
                                //第一行与第五行相等
                                if ((gvwStatisticsHos.Rows.Count - j) >= 5 && gvwStatisticsHos.Rows[j].Cells[0].Text == gvwStatisticsHos.Rows[j + 4].Cells[0].Text)
                                {
                                    //头合并
                                    gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 5;
                                    gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 3].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 4].Cells[0].Visible = false;
                                    j += 4;
                                }
                                else
                                {
                                    //头合并
                                    gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 4;
                                    gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                    gvwStatisticsHos.Rows[j + 3].Cells[0].Visible = false;
                                    j += 3;
                                }

                            }
                            else
                            {
                                //头合并
                                gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 3;
                                gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                                gvwStatisticsHos.Rows[j + 2].Cells[0].Visible = false;
                                j += 2;
                            }
                        }
                        else
                        {
                            //头合并
                            gvwStatisticsHos.Rows[j].Cells[0].RowSpan = 2;
                            gvwStatisticsHos.Rows[j + 1].Cells[0].Visible = false;
                            j += 1;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 绑定统计
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double distributionAmountTotal = 0;
            double wareHouseAmountTotal = 0;//入库总金额

            for (int i = 0; i < gvwStatisticsHos.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[2].Text);
                distributionAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[3].Text);
                wareHouseAmountTotal += double.Parse(gvwStatisticsHos.Rows[i].Cells[4].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + distributionAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

        }

        /// <summary>
        /// 曲线图
        /// 2011.4.10 张雄 create
        /// </summary>
        private void BindMSChat()
        {
            //空为初始页
            if (dt.Rows.Count > 0)
            {

                this.divshow.Visible = true;

                string monch = ""; //月份
                #region 左图

                //***********************2010基本药物******************/
                Series series3 = new Series("2010基本药物");
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                //***************** 2010现价品种*****************/
                Series series4 = new Series("2010限价品种");
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                //***************** 2010现价品种*****************/
                Series series5 = new Series("2010补充药品");
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;


                series3.Label = "#VAL{F}";
                series3.Color = Color.FromArgb(74, 172, 197);

                series4.Label = "#VAL{F}";
                series4.Color = Color.FromArgb(129, 101, 162);
                series5.Label = "#VAL{F}";
                series5.Color = Color.FromArgb(192, 80, 78);
                #endregion

                #region 绑定值 分3块, 3.2010基本药品目录，5.2010现价品种，6.2010补充目录（数量，采购金额）
                int s2 = 0, s3 = 0, s4 = 0;
                for (int i = 0; i < dt.Rows.Count; i++) //添加数据
                {

                    if (i == 0)    //初始化横轴
                    {
                        // serise1，series5基本药物
                        monch = dt.Rows[0]["daytime"].ToString();

                        #region 3.2010基本药品目录
                        if (dt.Rows[i]["SortSource"].ToString().Equals("3"))//2010基本药品目录
                        {
                            //金额
                            series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series3.Points[s2].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                            monch = dt.Rows[i]["daytime"].ToString();

                            series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series4.Points[s3].ToolTip = "0";
                            series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series5.Points[s4].ToolTip = "0";
                        }
                        #endregion

                        #region 5.2010现价品种
                        if (dt.Rows[i]["SortSource"].ToString().Equals("5"))//限价品种
                        {
                            //现价品种采购金额
                            series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series4.Points[s3].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                            monch = dt.Rows[i]["daytime"].ToString();

                            series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series3.Points[s2].ToolTip = "0";

                            series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series5.Points[s4].ToolTip = "0";
                        }
                        #endregion

                        #region 6.2010补充目录
                        if (dt.Rows[i]["SortSource"].ToString().Equals("6"))//补充药物
                        {
                            //金额
                            series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series5.Points[s4].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();


                            monch = dt.Rows[i]["daytime"].ToString();

                            series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series3.Points[s2].ToolTip = "0";

                            series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                            series4.Points[s3].ToolTip = "0";
                        }
                        #endregion
                        s2++;
                        s3++;
                        s4++;
                    }
                    else
                    {
                        if (dt.Rows[i]["daytime"].ToString().Equals(monch))//第一行和第2行是同一个月份
                        {
                            #region 2010基本药品目录
                            if (dt.Rows[i]["SortSource"].ToString().Equals("3"))//2010基本药品目录
                            {
                                series3.Points.RemoveAt(s2 - 1);
                                series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series3.Points[s2 - 1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                                monch = dt.Rows[i]["daytime"].ToString();

                            }
                            #endregion

                            #region 2010现价品种
                            if (dt.Rows[i]["SortSource"].ToString().Equals("5"))//判断是哪种采购类型
                            {
                                series4.Points.RemoveAt(s3 - 1);
                                series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series4.Points[s3 - 1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                                monch = dt.Rows[i]["daytime"].ToString();

                            }
                            #endregion

                            #region 2010补充目录
                            if (dt.Rows[i]["SortSource"].ToString().Equals("6"))//判断是哪种采购类型
                            {
                                series5.Points.RemoveAt(s4 - 1);
                                series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series5.Points[s4 - 1].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                                monch = dt.Rows[i]["daytime"].ToString();

                            }
                            #endregion
                        }
                        else
                        {
                            if (dt.Rows[i]["SortSource"].ToString().Equals("3"))//判断是哪种采购类型
                            {
                                series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series3.Points[s2].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                                series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series4.Points[s3].ToolTip = "0";

                                series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series5.Points[s4].ToolTip = "0";
                            }

                            if (dt.Rows[i]["SortSource"].ToString().Equals("5"))//判断是哪种采购类型
                            {
                                series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series4.Points[s3].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                                series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series3.Points[s2].ToolTip = "0";

                                series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series5.Points[s4].ToolTip = "0";

                            }

                            if (dt.Rows[i]["SortSource"].ToString().Equals("6"))//判断是哪种采购类型
                            {
                                series5.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                                series5.Points[s4].ToolTip = (double.Parse(dt.Rows[i]["PurchaseAmountTotal"].ToString()) / 10000).ToString();


                                series3.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series3.Points[s2].ToolTip = "0";


                                series4.Points.AddXY(dt.Rows[i]["daytime"].ToString() + "月", 0);
                                series4.Points[s3].ToolTip = "0";

                            }
                            monch = dt.Rows[i]["daytime"].ToString();
                            s2++;
                            s3++;
                            s4++;
                        }
                    }
                }
                #endregion

                #region 绑定到图
                series3.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series3);
                ChartPurchasing.Series["2010基本药物"].ChartArea = "ChartHosPurchasing";

                series4.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series4);
                ChartPurchasing.Series["2010限价品种"].ChartArea = "ChartHosPurchasing";

                series5.IsValueShownAsLabel = true;
                ChartPurchasing.Series.Add(series5);
                ChartPurchasing.Series["2010补充药品"].ChartArea = "ChartHosPurchasing";

                #endregion

                #region 两张图片属性

                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].Area3DStyle.Enable3D = false;

                ChartPurchasing.Series["2010基本药物"]["PointWidth"] = "0.3";
                ChartPurchasing.Series["2010限价品种"]["PointWidth"] = "0.3";
                ChartPurchasing.Series["2010补充药品"]["PointWidth"] = "0.3";


                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.MajorGrid.Interval = 1;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartHosPurchasing"].AxisX.Title = "月";

                #endregion

                #region 分组

                ChartPurchasing.Series["2010基本药物"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["2010限价品种"]["StackedGroupName"] = "Group1";
                ChartPurchasing.Series["2010补充药品"]["StackedGroupName"] = "Group1";

                ChartPurchasing.ResetAutoValues();
                #endregion
                lblnull.Visible = false;
            }
            else
            {
                //判断如果没数据则显示 “没数据”
                this.divshow.Visible = false;
                this.ChartPurchasing.Visible = false;
                this.lblnull.Visible = true;
            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!this.droYear.SelectedValue.Equals("-1") && !this.droMonth.SelectedValue.Equals("-1"))
            {
                this.month = int.Parse(this.droMonth.SelectedValue);
                this.year = int.Parse(this.droYear.SelectedValue);
                dataBind();
                BindMSChat();
                GetStatistics();
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择月')</script>");
            }
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

            string hospitalId = string.Intern(Hid);

            string Year = string.Intern(this.droYear.SelectedItem.Text);

            string Month = string.Intern(this.droMonth.SelectedValue);

            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();

            dt = ie.StatisticsHospitalByMonth(hospitalId, Year, Month);

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");//增加新列，主要目的是格式化显示字段

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwStatisticsHos);

        }
    }
}
